package com.yeyixiao.blog.service;

import java.util.*;

/*import com.yeyixiao.blog.bean.ChaptSectionBean;
import com.yeyixiao.blog.bean.ClassifyBean;*/
import com.yeyixiao.blog.common.page.Pagination;
import com.yeyixiao.blog.domain.Article;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.yeyixiao.blog.service.ArticleService;
import com.yeyixiao.blog.dao.ArticleDao;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service
public class ArticleService extends AbstractService<Article>{

    @Autowired
    private ArticleDao entityDao;

    /*@Autowired
    private CourseSectionService courseSectionService;*/
    public Pagination<Article> queryPage(Article queryEntity ,Pagination<Article> page){
        Integer itemsTotalCount = entityDao.getTotalItemsCount(queryEntity);
        List<Article> items = entityDao.queryPage(queryEntity,page);
        page.setItemsTotalCount(itemsTotalCount);
        page.setItems(items);
        return page;
    }
    public Article getById(Long id){
        return entityDao.getById(id);
    }

    public List<Article> queryAll(Article queryEntity){
        return entityDao.queryAll(queryEntity);
    }

    /*public Pagination<Article> queryPage(Article queryEntity ,Pagination<Article> page){
        Integer itemsTotalCount = entityDao.getTotalItemsCount(queryEntity);
        List<Article> items = entityDao.queryPage(queryEntity,page);
        page.setItemsTotalCount(itemsTotalCount);
        page.setItems(items);
        return page;
    }*/

    //基本信息初始化
    public void initCourse(Article entity){

        entity.setDate(new Date());
        entity.setPlay(0);entity.setLike(0);
        Integer art=getNewArticle();
        entity.setId(art+1);
    }

    public void create(Article entity){
        entity.setDate(new Date());
        //entity.setUpdateAt(new Date());
        entityDao.create(entity);
    }

    @Transactional
    public Article createCourse(Article entity/*, List<ChaptSectionBean> chaptSections*/){
        initCourse(entity);
        this.create(entity);

        /*if(!CollectionUtils.isEmpty(chaptSections)){
            //创建课程的章节数据
            Integer totalTime = courseSectionService.createCourseSections(entity.getId(),chaptSections);
            entity.setTime(totalTime.toString());//存的是时间的整数

        }*/
        this.update(entity);
        return entity;
    }

    public void update(Article entity){
        //entity.setDate(new Date());

        entityDao.update(entity);
    }

    public void delete(Article entity){
        entityDao.delete(entity);
    }
    public Integer getNewArticle(){
       List<Integer> list=entityDao.getNewArticle();
        Integer maxint=0;

        Iterator<Integer> iterator=list.iterator();
        while(iterator.hasNext()){
            Integer news=iterator.next();
            if(news >maxint){
                maxint=news;

            }
        }
        return maxint;
    }
    //更新课程时长
    /*public void updateCourseTime(Long courseId, Integer time){
        Course course = entityDao.getById(courseId);
        if(null != course && null != time){
            String courseTime = course.getTime();
            if(StringUtils.isNotEmpty(courseTime)){
                time = Integer.parseInt(courseTime) + time;
                course.setTime(time.toString());
                this.update(course);
            }
        }
    }
*/
    //准备分类对应的推荐课程数据
    /*public void prepareClassifyCourses(Map<String, ClassifyBean> classifyMap){
        for(String key : classifyMap.keySet()){
            Course course = new Course();
            course.setClassify(key);
            List<Course> courseList = this.entityDao.queryRecommendCourse(course);

            classifyMap.get(key).setRecommendCourseList(courseList);
        }
    }*/

    //获取名师公开课
    /*public List<Course> queryPublicCourses(){
        return entityDao.queryPublicCourses();
    }

    //获取直播课
    public List<Course> queryLiveCourses(){
        return entityDao.queryLiveCourses();
    }

    //系列课
    public List<Course> querySeriesCourses(){
        return entityDao.querySeriesCourses();
    }

    //精品课
    public List<Course> queryFreeCourses(){
        return entityDao.queryFreeCourses();
    }

    //最新课
    public List<Course> queryNewCourses(){
        return entityDao.queryNewCourses();
    }*/


}

